import { PageResponse, Response } from '@/types/Common';
import axios from '../request';
import { CertInfo, DataType } from '@/types/Cert';

const batchAddCert = async (clientId, certs) => {
  const body = {
    client_id: clientId,
    items: certs,
  };
  const res = await axios.post(`/Api/DataList/add`, body, {
    headers: {
      'Content-Type': 'application/json',
    },
  });
  return res.data;
};

export default {
  /**
   * @param {number} parentId
   * @return {Promise<Response<PageResponse<DataType[]>>>}
   */
  getDataType: async (parentId) => {
    const res = await axios.get(`/Api/DataType/getList?parentId=${parentId}`);
    return res.data;
  },
  /**
   *
   * @param {number} clientId 客户ID
   * @param {CertInfo} cert 证书信息
   * @return { Promise<Response<any>>}
   */
  addCert: async (clientId, cert, isCert) => {
    // 需要指定添加的是证书
    cert.is_cert = isCert;
    return await batchAddCert(clientId, [cert]);
  },
  batchAddCert,
  /**
   * @param {CertInfo} cert
   * @return {Promise<Response<any>>}
   */
  updateCert: async (clientId, cert) => {
    const res = await axios.post(
      `/Api/DataList/update?client_id=${clientId}`,
      cert
    );
    return res.data;
  },
  /**
   * @param {number} id
   * @return {Promise<Response<any>>}
   */
  deleteCert: async (id) => {
    const res = await axios.post(`/Api/DataList/delete?data_id=${id}`);
    return res.data;
  },
};
